package ducna.misscall.screen.contact.adapter;

/**
 * @author DUCNA
 */
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.Contacts;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AlphabetIndexer;
import android.widget.QuickContactBadge;
import android.widget.SectionIndexer;
import android.widget.TextView;
import ducna.alozo.misscall.R;
import ducna.misscall.screen.contact.image.ImageLoader;
import ducna.misscall.screen.contact.model.ContactsQuery;

public class ContactAdapter extends CursorAdapter implements SectionIndexer,
		ContactsQuery {
	private LayoutInflater mInflater;
	private ImageLoader mImageLoader;
	private AlphabetIndexer mAlphabetIndexer; // Stores the AlphabetIndexer
												// instance

	public ContactAdapter(Context context, ImageLoader mImageLoader) {
		super(context, null, 0);
		mInflater = LayoutInflater.from(context);
		this.mImageLoader = mImageLoader;
		final String alphabet = context.getString(R.string.alphabet);

		mAlphabetIndexer = new AlphabetIndexer(null, ContactsQuery.SORT_KEY,
				alphabet);

	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
		View row = mInflater.inflate(R.layout.item_list_contact, viewGroup,
				false);
		final ViewHolder holder = new ViewHolder();
		holder.textViewDisplayName = (TextView) row
				.findViewById(R.id.displayname);
		holder.quickContactBadge = (QuickContactBadge) row
				.findViewById(R.id.quickcontact);
		row.setTag(holder);
		return row;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final ViewHolder holder = (ViewHolder) view.getTag();

		String photoUri = cursor.getString(PHOTO_THUMBNAIL_DATA);
		String displayName = cursor.getString(DISPLAY_NAME);
		holder.textViewDisplayName.setText(displayName);
		// Generates the contact lookup Uri
		final Uri contactUri = Contacts.getLookupUri(
				cursor.getLong(ContactsQuery.ID),
				cursor.getString(ContactsQuery.LOOKUP_KEY));

		// Binds the contact's lookup Uri to the QuickContactBadge
		holder.quickContactBadge.assignContactUri(contactUri);

		mImageLoader.loadImage(contactUri, holder.quickContactBadge);
	}

	static class ViewHolder {
		private TextView textViewDisplayName;
		private QuickContactBadge quickContactBadge;
	}

	@Override
	public Cursor swapCursor(Cursor newCursor) {
		// Update the AlphabetIndexer with new cursor as well
		mAlphabetIndexer.setCursor(newCursor);
		return super.swapCursor(newCursor);
	}

	@Override
	public int getCount() {
		if (getCursor() == null) {
			return 0;
		}
		return super.getCount();
	}

	@Override
	public Object[] getSections() {
		return mAlphabetIndexer.getSections();
	}

	@Override
	public int getPositionForSection(int sectionIndex) {
		if (getCursor() == null) {
			return 0;
		}
		return mAlphabetIndexer.getPositionForSection(sectionIndex);
	}

	@Override
	public int getSectionForPosition(int position) {
		 if (getCursor() == null) {
             return 0;
         }
         return mAlphabetIndexer.getSectionForPosition(position);
	}
}
